package org.jeecg.modules.charge.controller;


import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.charge.entity.ChargeCollect;
import org.jeecg.modules.charge.entity.ChargeInvoiceType;
import org.jeecg.modules.charge.model.ChargeTables;
import org.jeecg.modules.charge.service.impl.ChargeBillsServiceImpl;
import org.jeecg.modules.charge.service.impl.ChargeInvoiceTypeServiceImpl;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @Description: 部门票据领用汇总表
* @Author: jeecg-boot
* @Date:   2019-10-29
* @Version: V1.0
*/
@Slf4j
@Api(tags="部门票据领用汇总表")
@RestController
@RequestMapping("/b/charge/collar")
public class ChargeInvoiceCollarController {

    @Autowired
    private ChargeInvoiceTypeServiceImpl chargeInvoiceTypeServiceImpl;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ChargeBillsServiceImpl chargeBillsService;
    /**
     * 票据类型
     * @param
     * @param req
     * @return
     */
    @GetMapping(value = "/getByInvoiceTables")
    public Result<List<ChargeTables>> getChargeBillsAll(HttpServletRequest req) {
        Result<List<ChargeTables>> result = new Result<List<ChargeTables>>();
        List<ChargeTables> tables = new ArrayList<ChargeTables>();
        try {
            List<ChargeInvoiceType> menthod =  chargeInvoiceTypeServiceImpl.getInvoiceType();
            for(ChargeInvoiceType th:menthod){
                ChargeTables chargeTables =new ChargeTables();
                chargeTables.setTitle(th.getName());
                chargeTables.setAlign("center");
                chargeTables.setWidth("100px");
                chargeTables.setDataIndex(StringUtils.getPinYinHeadChar(th.getName()));
                tables.add(chargeTables);
            }
            result.setSuccess(true);
            result.setResult(tables);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }
    /**
     * 部门票据领用汇总
     * @param
     * @param req
     * @return
     */
    @PostMapping(value = "/getInvoceCollar")
    public Result<List<ChargeCollect>> getInvoceCollar(@RequestBody ChargeCollect chargeCollect,
                                                      HttpServletRequest req) {
        Result<List<ChargeCollect>> result = new Result<List<ChargeCollect>>();
        try {
            List<ChargeCollect> collectList = new ArrayList<ChargeCollect>();
            if(chargeCollect.getProjectIds().size() > 0){
                for(String departId:chargeCollect.getProjectIds()){
                    chargeCollect.setProjectId(departId);
                    List<ChargeCollect> collect =  chargeBillsService.getCollarInvoice(chargeCollect); //获取领用票据信息
                    SysDepart sysDepartOne = sysDepartService.getById(departId);//获取部门信息
                    ChargeCollect ect = new ChargeCollect();
                    //判断是否有上级部门
                    String updeptName = "";
                    if (StringUtils.isNotEmpty(departId)) {
                        try {
                            SysDepart upSysDept = sysDepartService.getById(sysDepartOne.getParentId());
                            updeptName = upSysDept.getDepartName();
                        } catch (Exception e) {
                            log.error("没有父级   ", e.getMessage());
                        }
                    }
                    String deptName = updeptName + sysDepartOne.getDepartName();
                    if(collect != null && collect.size()> 0){
                        for (ChargeCollect co:collect){
                            co.setMethodName(StringUtils.getPinYinHeadChar(co.getMethodName()));
                        }
                        ect.setCollects(collect);
                        ect.setFeeName(deptName);
                        collectList.add(ect);
                    }
                }
            }
            result.setSuccess(true);
            result.setResult(collectList);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }


}
